package com.zyz.K_集合.Iterator迭代器;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
 * @author: vick
 * @Date: 2025/3/27
 * @Description:
 * @version: 1.0
 */
public class TestIterator3 {
    public static void main(String[] args) {
        Collection collection = new ArrayList();
        collection.add(111);
        collection.add(222);
        collection.add("vick");
        collection.add("aaa");
        Iterator iterator = collection.iterator();

        while (iterator.hasNext()) {
            Object next = iterator.next();
            System.out.println(next);
            //collection.remove("vick"); //抛出异常
            if (next == "vick") {
                //如果要删除元素，使用Iterator接口的remove方法
                iterator.remove();
            }
        }

        System.out.println(collection);
    }
}
